﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using elib = Microsoft.Practices.EnterpriseLibrary.Logging;
namespace Thallo {
    class Loger {
        const string Category = "thallo";
        /// <summary>
        /// 向指定的日志种类（日志种类指的是配置文件如：App.config里面配置的具体Logger种类）
        /// 写入一个新的带有调试信息的LogEntry.
        /// </summary>
        /// <param name="message">调试信息</param>
        public static void Debug(string message) {
            elib.Logger.Write(new elib.LogEntry() {
                Message = message,
                Categories = new string[] { Category },
                Severity = System.Diagnostics.TraceEventType.Verbose
            });
        }

        /// <summary>
        /// 向指定的日志种类（日志种类指的是配置文件如：App.config里面配置的具体Logger种类）
        /// 写入一个新的带有一般信息的LogEntry.
        /// </summary>
        /// <param name="message">一般信息</param>
        public static void TraceInformation(string message) {
            elib.Logger.Write(new elib.LogEntry() {
                Message = message,
                Categories = new string[] { Category },
                Severity = System.Diagnostics.TraceEventType.Information
            });
        }

        public static void TraceInformation(string format,params string[] vals) {
            TraceInformation(string.Format(format, vals));
        }

        /// <summary>
        /// 向指定的日志种类（日志种类指的是配置文件如：App.config里面配置的具体Logger种类）
        /// 写入一个新的带有错误信息的LogEntry.
        /// </summary>
        /// <param name="message">错误信息</param>
        public static void TraceError(string message) {
            elib.Logger.Write(new elib.LogEntry() {
                Message = message,
                Categories = new string[] { Category },
                Severity = System.Diagnostics.TraceEventType.Error
            });
        }

        /// <summary>
        /// 向指定的日志种类（日志种类指的是配置文件如：App.config里面配置的具体Logger种类）
        /// 写入一个新的带有错误异常信息的LogEntry.
        /// </summary>
        /// <param name="message">错误异常信息</param>
        /// <param name="ex">错误异常对象</param>
        public static void TraceError(string message, System.Exception ex) {
            elib.LogEntry le = new elib.LogEntry();
            le.Message = message;
            le.Priority = 2;
            le.Categories = new string[] { Category };
            le.Severity = System.Diagnostics.TraceEventType.Error;
            le.ExtendedProperties.Add("Exception", ex.Message);
            le.ExtendedProperties.Add("ExceptionType", ex.GetType().Name);
            le.ExtendedProperties.Add("CallStack", ex.StackTrace);
            elib.Logger.Write(le);
        }

        /// <summary>
        /// 向指定的日志种类（日志种类指的是配置文件如：App.config里面配置的具体Logger种类）
        /// 写入一个新的带有警告信息的LogEntry.
        /// </summary>
        /// <param name="message">警告信息</param>
        public static void TraceWarning(string message) {
            elib.Logger.Write(new elib.LogEntry() {
                Message = message,
                Categories = new string[] { Category },
                Severity = System.Diagnostics.TraceEventType.Warning
            });
        }

        public static void TraceWarning(string format ,params string[] values) {
            TraceWarning(string.Format(format, values));
        }
    }
}
